#include <iostream>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
using namespace std;

class File
{
public:
    File(char *filepath)
    {
        this->filepath = new char[100];
        strcpy(this->filepath, filepath);
        fd = open(filepath, O_RDWR | O_APPEND, 0755);
        if (fd == -1)
        {
            cout << "打开文件失败" << endl;
        }
        else
        {
            cout << "打开文件成功" << endl;
        }
    }

    ~File()
    {
        close(fd);
        delete[] filepath;
        cout << "收尾工作完成" << endl;
    }

    void fileWrite(char *buf)
    {
        write(fd, buf, 11);
        lseek(fd, 0, SEEK_SET);
    }
    void fileRead()
    {
        read(fd, buf, sizeof(buf));
        cout << buf << endl;
    }

private:
    char *filepath;
    int fd;
    char buf[100];
};

int main()
{

    char filepath[100] = "test.txt";
    File *file = new File(filepath);
    char buf[100] = "hello world";
    file->fileWrite(buf);
    file->fileRead();
    delete file;

    return 0;
}